import {elements} from "./elements.js";
import {soilImg} from "./resources.js";
import {constants} from "./constants.js";


class TerrariaMap{
    cubes = new Uint8Array(500 * 500).fill(0)
    #height = 50
    #width = 50

    /**
     * 创建一个方块
     * @param pos {{x:number,y:number}}
     */
    creat = (pos)=>{
        this.cubes[Math.floor(pos.y / 10) * 50 + Math.floor(pos.x / 10)] = 1;
        // console.log(pos,Math.floor(pos.y / 10) * 50 + Math.floor(pos.x / 10))
        this.draw(pos);
    }

    /**
     * 清除一个方块
     * @param pos {{x:number,y:number}}
     */
    delete = (pos)=>{
        this.cubes[Math.floor(pos.y / 10) * 50 + Math.floor(pos.x / 10)] = 0;
        this.clearACube(pos);
    }

    draw = (pos)=>{
        elements.backGroundContext.drawImage(soilImg, pos.x, pos.y)
    }

    clearACube = (pos)=>{
        elements.backGroundContext.clearRect(pos.x, pos.y, constants.CUBE_LENGTH, constants.CUBE_LENGTH);
    }

    getCode = (x, y)=>{
        // 超出界限
        if (x < 0 || x >= this.#width){
            return -1
        }
        return this.cubes[x + y * 50]??1
    }

}

export const terrariaMap = new TerrariaMap();